<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/10/10  10:18
 */
namespace app\admin\controller;
use think\Db;
use think\Controller;
use app\admin\controller\Export;
class Member extends Common
{
    private $table_admin;
    /*
    *   构造函数
    */
    function __construct(){
        parent::__construct();
        $this->table_admin = db('member');
    }

//陶一会员
    public function lists(){
        $phone = input('phone');

        if(!empty($phone)){

            $where['phone']= array('like','%'.input('phone').'%');
        }else{
            $where ='';
        }

        $users = db('user_account')->where('status !=1')->where($where)->paginate();
        $this->assign('users',$users);
        $this->assign('page', $users->render());
        return $this->fetch();
    }

//陶一会员2
    public function lists_info(){

        $phone = input('phone');

        if(!empty($phone)){
            $where['phone']= array('like','%'.input('phone').'%');
        }else{
            $where ='';
        }

        $users = db('user_account')->where('status !=1')->where($where)->paginate()->each(function($item, $key){

            if(!empty($item['recommend_id'])){

                $item['recommend_name'] = db('user_account')->where('id',$item['recommend_id'])->value('account');      
            }else{
                $item['recommend_name'] = '总店';
            }

            return $item;
        });
 /*       dump($users);die;*/
        $this->assign('users',$users);
        $this->assign('page', $users->render());
        return $this->fetch();
}


//会员详情    
    public function member_info(){

        $phone = input('phone');

        if(!empty($phone)){
            $where['phone']=$phone;
        }else{
            $where ='';
        }

        $uid = input('uid');
      /* dump($uid);die;
*/
        $users = db('user_account')->where('status!=1')->where($where)->where('id',$uid)->select();
/*        dump($users);die;*/
       

        $data = $this->next_member($uid);



        $this->assign('users',$users);
        $this->assign('team',$data);
        return $this->fetch();
    }

//会员等级    
    public function member_level(){
        $level = db('member')->select();
        $this->assign('level',$level);
        return $this->fetch();
    }

//会员等级修改    
    public function edit_level(){

        $id = input('id');
        if(!empty($id)){
            $level = db('member')->where('id',$id)->find();

            $this->assign('da',$level);
        }   
       
        return $this->fetch();
    }

 
    public function doedit_level(){
        
        $id = input('id');
        $level = input('level');
        $price = input('price'); 
        $details = input('details');
        $time = time();
        $data['level'] = $level;

        if($level==1){
            $data['name'] = '普通会员'; 
        }elseif ($level==2){
            $data['name'] = '高级会员';
        }

        $data['price'] = $price;
        $data['details'] = $details;

        //每日可接任务数
        $data['task_num'] = input('task_num');

        if(!empty($data['task_num'])){

            //取出原来次数
            $user2 = db('user_account')->where('level',$level)->select();

            foreach ($user2 as $key => $value) {

                $data_num['day_num'] =0;

                $cj =0;

                if($value['day_num']<$data['task_num']){

                    $cj = $data['task_num']-$value['day_num'];//增多次数

                    $data_num['day_num'] = $value['day_num']+$cj;

                    db('user_account')->where('id',$value['id'])->where('level',$level)->update($data_num);

                }/*else{

                    $data_num['day_num'] = $value['day_num']-$data['task_num'];

                    if($data_num['day_num']<0){

                        $data_num['day_num'] =0;

                    }
                }*/

               
                
                
            }   

            
        }

   /*     $day_num['day_num'] = $data['task_num'];*/
      

        $data['created_at'] = $time;

        $file = request()->file('img_url');

            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public/static/uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['img_url'] =  'http://'.$_SERVER['HTTP_HOST'] . '/static/uploads/'.$info->getSaveName();
                    if(!empty($id)){
                        $oldimg = db('member')->where(['id'=>$id])->value('img_url');
                        $oldimgurl = ROOT_PATH . 'public/static/uploads/'.$oldimg;
                     /*   if(file_exists($oldimgurl)){
                            unlink($oldimgurl);
                        }*/
                    }
                }else{
                    // 上传失败获取错误信息
                    return json(['msg'=>'上传失败']);
                }
            }

            if(empty($id)){
                //添加
                  //更新购买这套餐的用户
                
       
                $data['created_at'] = time();
                $res = db('member')->insert($data);
            }else{
                //修改
                $res = db('member')->where(['id'=>$id])->update($data);
                //如果更新次数要更新用户表
            
            }
            
            if(empty($res))return json(['msg'=>'更新失败']);
            return json(['msg'=>'更新成功']);
    }         


//区域代理列表
    public function regional_lists(){

        return $this->fetch();
    }

/**
    * 商户导出excel
*/
    public function export()
    {

        

        

        $list = db("user_account")->select();



        if(empty($list)){
            $this->error('数据为空');
        }

        foreach ($list as $key => $value) {

            $data[$key]['account'] = $value['account'];

            $data[$key]['phone'] = $value['phone'];

            switch ($value['level']) {
                case '0':
                    $data[$key]['new_level'] = '实习成员';
                    break;
                case '1':
                    $data[$key]['new_level'] = '普通会员';
                break;
                case '2':
                    $data[$key]['new_level'] = '高级会员';
                break;

                default:
                    $data[$key]['new_level'] ='';
                    break;
            }

            $data[$key]['address'] = $value['province'].$value['city'].$value['county'];

            $data[$key]['wx'] = $value['wx'];

            $data[$key]['real_name'] = $value['real_name'];

            $data[$key]['created_at'] = date('Y-m-d h:i:s',$value['created_at']);
            
        }

        $headArr = array("会员账号","手机号码","会员等级","地区","微信","真实姓名","注册时间");
/*
       dump($list);exit();*/
        $data=array_filter($data);
        ///var_dump($data);exit();
         $filename='会员信息-'.date("Y-m-d H:i:s");
        (new Export())->ImportExcel($filename,$headArr,$data);
    }

    public function del_member(){

        $id = input('id');

        $status['status'] = 1;

        $res = db('user_account')->where('id',$id)->update($status);

        if(empty($res))return json(['msg'=>'更新失败']);
        return json(['msg'=>'更新成功']);
        
    }

}

